//==================l==============================
J𑜓xX=640
J𑜓xY=480
TCYX=320
TCYY=240
͎=300
BeFPS=10.0
O=""
Path=""
//=======================================================
//{\tg̎dlɓĈȉ̓_ɒӂĂ=====
//J̉𑜓xCameraResolutionCheckerŒׂ
//ׂƘ^悪Xgbv͂̂ߌׂł͂Ȃ
//4cam܂ŃT|[g,4͓𑜓xłKv,kMpeg-4ǂ
//Windows̎v␳@\؂ȂƎԂi񂾏ꍇAAVb^[؂t[Y̋UzɂȂĂ܂ߒ!
//^悪ԂɍȂꍇ^𑜓x(640x480ȉ)
//J҃=============================================
//Surface pro 3 camera res: 2592x1944(but actually works at 640x480), 1920x1080, 30fps 

#include "hspdsc.as"	//USBJLv`p̃vOC
#include "hspcv.as"		//C[Wp̃vOC
#include "hspext.as"

screen 0,840,150,0,100,20
title "AviRecorder"
mes "Enter camera resolution"

exist "CamResolutionX.ar"
if strsize!=-1{
	bload "CamResolutionX.ar",J𑜓xX
}
exist "CamResolutionY.ar"
if strsize!=-1{
	bload "CamResolutionY.ar",J𑜓xY
}

pos 10,25: mes "x:"
pos 30,25:	input J𑜓xX
pos 10,50: mes "y:"
pos 30,50:	input J𑜓xY
pos 10,75:	button "Next",*NextPhase
pos 0,105:	mes "* Use CameraResolutionChecker for the check of the camera resolution."
pos 0,120:	mes "* Xvid is required for the compressed video. Download it from https://www.xvid.com/ if you don't have it."
stop

*NextPhase
bsave "CamResolutionX.ar",J𑜓xX
bsave "CamResolutionY.ar",J𑜓xY

exist "VideoSizeX.ar"
if strsize!=-1{
	bload "VideoSizeX.ar",TCYX
}
exist "VideoSizeY.ar"
if strsize!=-1{
	bload "VideoSizeY.ar",TCYY
}

exist "Name.ar"
if strsize!=-1{
	bload "Name.ar",O
}
exist "RecordFPS.ar"
if strsize!=-1{
	bload "RecordFPS.ar",BeFPS
}
exist "RecordDur.ar"
if strsize!=-1{
	bload "RecordDur.ar",͎
}
exist "Path.ar"
if strsize!=-1{
	bload "Path.ar",Path
}

ʕ=1366		//pl\EBhEXTCY
ʍ=768

J=0	//FJ̐
dim ʒuX,11
dim ʒuY,11


쑋=840
쑋=113
ʒuX(0)=80
ʒuY(0)=5
ʒuX(10)=80
ʒuY(10)=155
screen 0,쑋,쑋,0,ʒuX(0),ʒuY(0)
buffer 2,100,100
buffer 3,100,100
buffer 4,100,100
buffer 5,100,100
buffer 6,J𑜓xX,J𑜓xY,0,ʒuX(0),ʒuY(0)
title "6"
buffer 7,J𑜓xX,J𑜓xY,0,ʒuX(0),500
title "7"
buffer 8,J𑜓xX,J𑜓xY,0,ʒuX(0),ʒuY(0)
title "8"
buffer 9,J𑜓xX,J𑜓xY,0,ʒuX(0),500
title "9"
screen 10,TCYX*2,TCYY,0,ʒuX(10),ʒuY(10)	//W
buffer 11, 2000,4096,0//,500,10	//offsetp

NextShatterTime=0.0	//ɃVb^[؂鎞
ShatterWaitTime=0.0	//Vb^[Ԃ̎
ElapsedTime=0.0	//\tgNĂ̌oߎ
ElapsedTimePerFrame=0.0
VideoLength=0.0	//̒(sec)
StartElapsedTime = 0.0	//^Jňoߎێ

ABet[=0

Path="No path"	//̕ϐŎꂽpXɉ͌ʂ⓮悪ۑ
exist "Path"
if strsize!=-1{
	bload "Path",Path	//[h
}

//JX^[g
dsc_Init					//KI
sdim CamName,1024
dsc_GetDeviceList CamName		//USBJXg̕ϐɕێ
if CamName=""{
	dialog "Device is not found"
	end
}
J=stat
//

gosub *TCYXV
screen 10,܂ƂߓTCYX,܂ƂߓTCYY,0,ʒuX(10),ʒuY(10)	//W
gosub *擾Jn

gsel 0
UiX=180
{^Y=2
pos 0,-100:	input 
objsize 50,25
pos UiX,{^Y:	input TCYX:	{^Y+=25
pos UiX,{^Y:	input TCYY:	{^Y+=28
objsize 220,25
pos 5,{^Y:	button gosub "Apply",*擾XV:{^Y+=75
color 150,150,150
line 235,0,235,82
objsize 190,25
UiX=450:{^Y=2
pos UiX,{^Y:	input O:	{^Y+=25
pos UiX,{^Y:	input BeFPS:	{^Y+=25
pos UiX,{^Y:	input ͎:	{^Y+=25

color 150,150,150
line 645,0,645,82
objsize 100,25
UiX=650:{^Y=2
pos UiX,{^Y:	button gosub "REC Start",*^Jn:{^Y+=25
pos UiX,{^Y:	button gosub "REC Stop",*^I:{^Y+=25
objsize 80,25
UiX=2:{^Y=85
pos UiX,{^Y:	button gosub "Select folder",*ۑꏊI:{^Y+=25
color 0,0,0
UiX=2
{^Y=2
pos UiX,{^Y:	mes "Output X size (pixel):":	{^Y+=25
pos UiX,{^Y:	mes "Output Y size (pixel):":	{^Y=2
UiX=250
pos UiX,{^Y:	mes "File name:":	{^Y+=25
pos UiX,{^Y:	mes "Frame/sec of out movie:":	{^Y+=25
pos UiX,{^Y:	mes "Capture duration(sec):":	{^Y+=25

onexit *Exit
^揉t[=1	//^̈ԍŏ̃t[̂1
^ŏ҂Cnt=0
^Phase=0
color 150,150,150
line 0,82,쑋,82
gosub *pX\

PreTime=0.0
CurrTime=0.0

OffsetInit=0
REPEAT
	N=gettime(0):=gettime(1):=gettime(3):=gettime(4):=gettime(5):b=gettime(6)
	mx=ginfo(0)-ginfo(4)-3
	my=ginfo(1)-ginfo(5)-25
	stick st
	if st=128{
		dsc_SetActiveSel 0
		dsc_Stop	//VKǉ
		dsc_Close
		break
	}
	
	Repeat J		//DSCJ̉fHSPֈڍs
		gsel (6+cnt)
		pos 0,0
		dsc_SetActiveSel cnt
		//dsc_GetImage hdc,0,-J𑜓xY/4,J𑜓xX,J𑜓xY	//USBJ摜̎擾 X̾,YItZbg,,
		dsc_GetImage hdc,0,0,J𑜓xX,J𑜓xY	//USBJ摜̎擾 X̾,YItZbg,,
		
		if OffsetInit=0{
			OffsetInit=1
			gsel 11
			pos 0,0
			gcopy (6+cnt),0,0,2000,4096
			Repeat 4096
				pget 0,cnt
				if ginfo(16)!=255 | ginfo(17)!=255 | ginfo(18)!=255{
					OffsetY=cnt
					title ""+OffsetY
					break
				}
				
			Loop
		}
		
		redraw 1
		redraw 0
	Loop
	gsel 10
	
	Repeat J	//fkÃJfPɂ܂Ƃ߂
		if cnt=0:pos 0,0
		if cnt=1:pos TCYX,0
		if cnt=2:pos 0,TCYY
		if cnt=3:pos TCYX,TCYY
		gzoom TCYX,TCYY,(6+cnt),0,OffsetY,J𑜓xX,J𑜓xY,0	//k쐬A^Cɕׂ
	Loop
	
	if ^撆=1{
		if ^Phase=1{
			^ŏ҂Cnt++
			if ^ŏ҂Cnt>=10{
				^Phase=2
				NextShatterTime = ElapsedTime
				StartElapsedTime = ElapsedTime
			}
		}
		VideoLength=ElapsedTime-StartElapsedTime
		if ^Phase=2{
			if NextShatterTime <= ElapsedTime{
				gsel 10
				cvputimg 0
				cvputavi
				ShatterWaitTime = 1000.0/double(BeFPS)	//FPS玟̎Be܂ł̑҂ԂvZ
				NextShatterTime += ShatterWaitTime
				//ABet[++
				//VideoLength += ElapsedTimePerFrame	//^掞ԂvZ
				//if ABet[=2:stop
				//if ABet[ >= ͎*BeFPS{

				//title ""+VideoLength+" "+͎*1000
				if VideoLength >= ͎*1000{
					gosub *^I
				}
				if ^撆=1{
					ABet[++
				}
			}
			color 255,0,0
			circle 10,10,25,25	//^}[N`
			pos 30,10
			mes "REC"	//Show REC label
			
			redraw 1
			redraw 0
		}
	}
	//1frameɊ|ԂƃvONĂ|ԂvZ

	
	CurrTime = gettime(6)*1000 + gettime(7)
	ElapsedTimePerFrame = 0.0
	if(CurrTime >= PreTime){	//JオĂȂ
		ElapsedTimePerFrame = CurrTime - PreTime	//Oframe獡frame܂łɊ|ԂZo
	}
	if(CurrTime < PreTime){	//JオĂ
		ElapsedTimePerFrame = 60000-PreTime + CurrTime	//Oframe獡frame܂łɊ|ԂZo
	}
	if ElapsedTimePerFrame > 2000.0{	//Windows̎v߂ĈُȌJオ菈NĂ܂(1frame = 5secƂ蓾Ȃlo)
		ElapsedTimePerFrame =0.0
	}
	ElapsedTime += ElapsedTimePerFrame	//^JnĂ|(ms)XV
	
	
		//title ""+J+" "+ElapsedTimePerFrame+" n"+NextShatterTime+" e"+ElapsedTime+" "+CurrTime
	title ""+ABet[+" frames  "+(VideoLength/1000.0)+" sec"
		//title ""+gettime(6)
	PreTime=CurrTime
	gsel 0
	//color 255,255,255
	//boxf 90,88,800,113
	
	//title " x"+mx+" y"+my
	//title " "+ABet[
	title "CamNum:"+J
	gsel 10
	redraw 1
	redraw 0
	
	await 1
LOOP
stop

*TCYXV
	if J=1{
		܂ƂߓTCYX=TCYX
		܂ƂߓTCYY=TCYY
	}
	if J=2{
		܂ƂߓTCYX=TCYX*2
		܂ƂߓTCYY=TCYY
	}
	if J>=3{
		܂ƂߓTCYX=TCYX*2
		܂ƂߓTCYY=TCYY*2
	}
	buffer 6,J𑜓xX,J𑜓xY,0
	buffer 7,J𑜓xX,J𑜓xY,0
	buffer 8,J𑜓xX,J𑜓xY,0
	buffer 9,J𑜓xX,J𑜓xY,0
	screen 10,܂ƂߓTCYX,܂ƂߓTCYY,0,ʒuX(10),ʒuY(10)
	cvbuffer 0,܂ƂߓTCYX,܂ƂߓTCYY
	OffsetInit=0
	return

*擾Jn
	REPEAT J
		dsc_SetActiveSel cnt
		dsc_Open cnt,0,0,0
		gsel (cnt+2)
		dsc_Play hwnd,0,0,0,0	//̏łHSPOSŕ\邾HSPɃf[^荞܂ȂA荞ނɂdsc_GetImageg
		//dsc_Play2
	LOOP
	cvbuffer 0,܂ƂߓTCYX,܂ƂߓTCYY
	return
	
*擾~	//[NȂ
	REPEAT J
		dsc_SetActiveSel cnt
		dsc_Stop
		dsc_Close
	LOOP
	return
	
*擾XV
	//gosub *擾~
	//gosub *擾Jn
	gosub *TCYXV
	bsave "VideoSizeX.ar",TCYX
	bsave "VideoSizeY.ar",TCYY
	return
	
*ۑꏊI
	selfolder  Path,"" //
	bsave "Path",Path
	CurrentActiveWindowID=ginfo(2)
	gosub *pX\
	gsel CurrentActiveWindowID
	gosub *p[^ۑ
	return
	
*pX\
	gsel 0
	color 255,255,255
	boxf 0,88,쑋,쑋
	color 0,0,0
	pos 90,88
	mes Path
	return
	
*^Jn
	dirlist x, Path, 5
	if stat = 0{
		dialog "Save folder is not found !" 
		return
	}
	//cvmakeavi Path+"\\"+O+" "+N+"_"++"_"++" "++"_"++"_"+b+".avi",0x4356534D,BeFPS,0	//path, codec, fps, CV buffer ID
	//cvmakeavi Path+"\\"+O+" "+N+"_"++"_"++" "++"_"++"_"+b+".avi",-1,BeFPS,0	//path, codec, fps, CV buffer ID
	cvmakeavi Path+"\\"+O+" "+N+"_"++"_"++" "++"_"++"_"+b+".avi",$44495658,BeFPS,0	//path, codec("xvid"), fps, CV buffer ID
	^揉t[=1
	gsel 10,1
	ABet[=0
	^撆=1
	^Phase=1
	^ŏ҂Cnt=0
	gosub *p[^ۑ
	return
	
*p[^ۑ
	bsave "VideoSizeX.ar",TCYX
	bsave "VideoSizeY.ar",TCYY
	bsave "Name.ar",O
	bsave "RecordFPS.ar",BeFPS
	bsave "RecordDur.ar",͎
	bsave "Path.ar",Path
	return
	
*^I
	cvendavi
	^撆=0
	return
	
*Exit
	gosub *擾~
	end